﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Account;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Core.Contracts.Finance;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Finance;
using TomNet.IMSC.Web.Areas.Finance.Controllers;
using TomNet.IMSC.Web.Areas.Finance.Models;
using TomNet.IMSC.Web.Common;
using TomNet.IMSC.Web.Filters;
using TomNet.IMSC.Web.Models.ApiParams;
using TomNet.Web.Mvc.UI;
using TomNet.Web.WebApi;

namespace TomNet.IMSC.Web.Controllers.API
{
    [Description("API-金融")]
    [ApiAuthentication]
    public class FinanceController : BaseApiController
    {

        #region =========================property


        public IFinanceRunningsContract Contract { get; set; }  //金融流水
        public IFinanceItemsContract ItemContract { get; set; } //金融项目
        public IFinanceItemsAttrContract ItemAttrContract { get; set; }
        public IUsersContract UserContract { get; set; }    //经纪人
        public IAgentCityContract ACContract { set; get; }//    代理开启城市

        public IIntermediaryContract InterContract { set; get; }    //中介
        public IIntermediaryStaffContract IISContract { get; set; } //中介业务员

        public ICustomerPhoneContract CPContract { get; set; } //客户多电话
        public ICustomerContract CContract { get; set; }    //客户
        //public IImagesContract ImagesContract { get; set; }//图片


        public IFinanceBusinessLoanRunningsContract BusinessContract { set; get; } ///***商贷购房
        public IFinanceTransferRunningsContract TransferContract { set; get; } ///***过户
        public IFinancePublicAccumulationFundsRunningsContract FundsContract { get; set; }///***公积金
        public IBrokerageSettleAccountsRunningsContract BrokerageSettleAccountsRunningsContract { get; set; }//佣金管理
        public IBrokerageRunningsContract BrokerageRunningsContract { get; set; }//佣金流水
        public IMessageBoardsContract MessageBoardsContract { get; set; }//报备跟踪信息
        public IStaffContract StaffContract { get; set; }//公司业务员

        public IUsersRolesContract UserRolesContract { get; set; }

        #endregion

        [Description("API-金融项列表-2")]
        public IHttpActionResult ItemList0([FromBody]ReqItem entity)
        {
            if (entity == null)
            {
                return Token(new AjaxResult("参数错误", AjaxResultType.Error), "");
            }

            var query = ItemContract.Entities.Where(m => !m.IsDeleted && m.State == 1 && m.ParentID == entity.ParentId);

            var data = query.OrderBy(x => x.ShowIndex).Select(m => new
            {
                text = m.Name,
                id = m.Id,
                icon = m.Icon == null ? "" : "/images/icon/" + m.Icon,
                attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == m.Id).Select(x => new { x.Id, x.AttrKey, x.AttrValue, x.Name }),
                mode = m.Masterplate,
                children = ItemContract.Entities.Where(a => !a.IsDeleted && a.State == 1 && a.ParentID.Equals(m.Id))
                //.Count()
                .OrderBy(x => x.ShowIndex).Select(mm => new
                {
                    text = mm.Name,
                    id = mm.Id,
                    icon = mm.Icon == null ? "" : "/images/icon/" + mm.Icon,
                    attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == mm.Id).Select(x => new { x.Id, x.AttrKey, x.AttrValue, x.Name }),
                    mode = mm.Masterplate,
                    children = ItemContract.Entities.Where(a => !a.IsDeleted && a.State == 1 && a.ParentID.Equals(mm.Id)).ToList()
                }).ToList()
            }).ToList();


            var result = new AjaxResult("成功", AjaxResultType.Success, data);
            return Token(result, "");
        }


        [Description("API-金融项列表")]
        public IHttpActionResult ItemBy([FromBody]ReqItem entity)
        {
            if (entity == null)
            {
                return Token(new AjaxResult("参数错误", AjaxResultType.Error), "");
            }

            var query = ItemContract.Entities.Where(m => !m.IsDeleted && m.State == 1 && m.Id == entity.ParentId);

            var data = query//.OrderBy(x => x.ShowIndex)
                .Select(m => new
                {
                    text = m.Name,
                    id = m.Id,
                    icon = m.Icon == null ? "" : "/images/icon/" + m.Icon,
                    attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == m.Id).Select(x => new { x.Id, x.AttrKey, x.AttrValue, x.Name }),
                    mode = m.Masterplate,
                    //children = ItemContract.Entities.Where(a => !a.IsDeleted && a.State == 1 && a.ParentID.Equals(m.Id))
                    ////.Count()
                    //.OrderBy(x => x.ShowIndex).Select(mm => new
                    //{
                    //    text = mm.Name,
                    //    id = mm.Id,
                    //    icon = mm.Icon == null ? "" : "/images/icon/" + mm.Icon,
                    //    attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == mm.Id).Select(x => new { x.Id, x.AttrKey, x.AttrValue, x.Name }),
                    //    mode = mm.Masterplate,
                    //    children = ItemContract.Entities.Where(a => !a.IsDeleted && a.State == 1 && a.ParentID.Equals(mm.Id)).ToList()
                    //}).ToList()
                }).FirstOrDefault();


            var result = new AjaxResult("成功", AjaxResultType.Success, data);
            return Token(result, "");
        }

        #region =========================for client at pc

        [Description("API-金融项列表-ALL")]
        public IHttpActionResult ExcludeTree()
        {
            var query = ItemContract.Entities.Where(m => !m.IsDeleted && m.State == 1 && m.ParentID == 0);

            Func<int, List<MyTree>> MingPian = null;
            MingPian = id =>
            {
                var basmas = ItemContract.Entities.Where(m => !m.IsDeleted && m.State == 1 && m.ParentID == id)
                .OrderBy(x => x.ShowIndex).Select(m => new MyTree
                {
                    parentId = m.ParentID,
                    id = m.Id + "",
                    text = m.Name,
                    state = new
                    {
                        opened = true,//展示第一个层级下面的node 
                    },
                    icon = m.Icon == null ? "" : "/images/icon/" + m.Icon,
                }).ToList(); ;
                foreach (var it in basmas)
                {
                    it.children = MingPian(int.Parse(it.id));
                }
                return basmas;
            };
            List<MyTree> ttree = MingPian(0);

            var result = new AjaxResult("成功", AjaxResultType.Success, ttree);
            return Token(result, "");
        }

        [Description("API-金融项列表-ALL")]
        public IHttpActionResult ItemAttrBy([FromBody]BaseEntity entity)
        {
            var data = (from m in ItemContract.Entities
                            //join n in ItemAttrContract.Entities on m.Id equals n.FinanceItemsID into tt from a in tt.DefaultIfEmpty()
                        where m.Id == entity.Id
                        select new
                        {
                            id = m.Id,
                            text = m.Name,
                            icon = m.Icon,
                            attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == m.Id).Select(y => new { y.Name, y.AttrKey, y.AttrValue }).ToList(),

                        }).ToList();

            var result = new AjaxResult("成功", AjaxResultType.Success, data);
            return Token(result, "");
        }

        #endregion


        #region =========================for client at mobile

        [Description("API-金融项列表")]
        public IHttpActionResult ItemList([FromBody]ReqItem entity)
        {
            if (entity == null)
            {
                return Token(new AjaxResult("参数错误", AjaxResultType.Error), "");
            }

            var query = ItemContract.Entities.Where(m => !m.IsDeleted && m.State == 1 && m.ParentID == entity.ParentId);

            var data = query.OrderBy(x => x.ShowIndex).Select(m => new
            {
                text = m.Name,
                id = m.Id,
                icon = m.Icon == null ? "" : "/images/icon/" + m.Icon,
                attr = ItemAttrContract.Entities.Where(x => x.FinanceItemsID == m.Id).Select(x => new { x.Id, x.AttrKey, x.AttrValue, x.Name }),
                mode = m.Masterplate,
                children = ItemContract.Entities.Where(a => !a.IsDeleted && a.State == 1 && a.ParentID.Equals(m.Id)).Count()
            }).ToList();


            var result = new AjaxResult("成功", AjaxResultType.Success, data);
            return Token(result, "");
        }

        [Description("API-金融报备")]
        public IHttpActionResult AddRunning([FromBody]RevengeUser entity)
        {
            try
            {
                //获取UserID
                int userid = BaseWebApiExt.GetUserIDByToken();

                UserContract.Repository.UnitOfWork.BeginTransaction();
                var itemObj = ItemContract.Entities.Where(x => x.Id == entity.RunningId).FirstOrDefault();//报备的 金融项目

                var cquery = (from m in CPContract.Entities
                              join p in CContract.Entities on m.CustomerID equals p.Id into tt
                              from a in tt.DefaultIfEmpty()
                              where m.Id == entity.PhoneId && !m.IsDeleted && !a.IsDeleted
                              select new
                              {
                                  m.Phone,
                                  a.Name,
                                  a.Id,
                              }).FirstOrDefault();


                var query = (from m in UserContract.Entities//.Where(x => x.Id == entity.UserId)
                             join p in IISContract.Entities on m.IntermediaryID equals p.IntermediaryID into tt
                             from a in tt.DefaultIfEmpty()
                             join p2 in InterContract.Entities on m.IntermediaryID equals p2.Id into tt2
                             from b in tt2.DefaultIfEmpty()
                             join p3 in ACContract.Entities on m.CityID equals p3.CityID into tt3
                             from c in tt3.DefaultIfEmpty()
                                 //用户未删除  //中介未删除
                             where m.Id == userid && !m.IsDeleted && !b.IsDeleted
                             select new
                             {
                                 a.StaffID,
                                 IntermediaryId = b.Id,//中介ID
                                 IntermediaryName = b.Name,//中介名称

                                 m.CityID,

                                 m.Id,  //用户信息
                                 m.Name,
                                 m.Phone,

                             }).FirstOrDefault();

                var agentObj = ACContract.Entities.Where(x => x.CityID == query.CityID).FirstOrDefault();//


                FinanceRunnings running = new FinanceRunnings();
                running.UserID = query.Id;
                running.UserName = query.Name;// "";
                running.UserPhone = query.Phone;// "";//经纪人相关信息

                running.AgentID = agentObj.AgentID;//代理Id
                running.Address = "";


                running.CollectDataRemark = "";
                running.CollectDataStaffID = 0;
                running.CollectDataState = 0;
                running.CollectDataTime = DateTime.Now;
                running.CollectDataType = 0;

                running.CustomerID = cquery.Id; //根据电话id 查询
                running.CustomerName = cquery.Name;
                running.CustomerPhone = cquery.Phone;

                running.FilingAuditingRemark = "";
                running.FilingAuditingStaffID = 0;
                running.FilingAuditingTime = DateTime.Now;

                running.FilingState = 1;//报备 未处理
                running.FilingTime = DateTime.Now;
                running.FinanceItemsID = itemObj.Id; //根据报备项id 查询
                running.FinanceItemsName = itemObj.Name;// "";

                running.HouseOwnerName = "";
                running.HouseOwnerPhone = "";

                running.IntermediaryID = query.IntermediaryId;
                running.IntermediaryName = query.IntermediaryName;//"";//中介信息
                running.Price = 0;
                running.Area = 0;
                running.BuildingName = "";

                running.Progress = 1;//报备 未处理

                running.TransferDetailed = 0;
                running.TransferDetailedRemark = "";

                running.SellStaffID = query.StaffID;//对应中介的管理人员ID
                running.TransactStaffID = 0;
                running.State = 1;
                running.Remark = "";

                Contract.Insert(running);
                //int[] _arr = new int[] { (int)LoanType.金融过户市件, (int)LoanType.金融过户区件, };
                int tmp = 0;
                if ((LoanType)Enum.ToObject(typeof(LoanType), itemObj.Id) == LoanType.商业贷款)
                {
                    FinanceBusinessLoanRunnings business = new FinanceBusinessLoanRunnings();
                    business.FinanceRunningsID = running.Id;
                    //business.ApproveRemark = "";    //审批信息
                    //business.ApproveStaffID = 0;
                    //business.ApproveState = 0;
                    business.ApproveTime = DateTime.Now;


                    //business.ContractRemark = "";//面签信息
                    //business.ContractStaffID = 0;
                    //business.ContractState = 0;
                    business.ContractTime = DateTime.Now;

                    //business.LoanAmount = 0;    //贷款信息
                    //business.LoanBank = "";
                    //business.LoanYearNumber = 0;

                    //business.ProvideRemark = "";//放款信息
                    //business.ProvideStaffID = 0;
                    //business.ProvideState = 0;
                    business.ProvideTime = DateTime.Now;

                    tmp = BusinessContract.Insert(business);
                }
                else
                {
                    switch ((LoanType)Enum.ToObject(typeof(LoanType), itemObj.ParentID))
                    {
                        case LoanType.公积金贷款:
                            FinancePublicAccumulationFundsRunnings funds = new FinancePublicAccumulationFundsRunnings()
                            {
                                ContractTime = DateTime.Now,
                                FinanceRunningsID = running.Id,
                                MortgageTime = DateTime.Now,
                                ProvideTime = DateTime.Now
                            };

                            tmp = FundsContract.Insert(funds);

                            break;
                        case LoanType.金融过户:
                            FinanceTransferRunnings obj = new FinanceTransferRunnings();
                            //obj.Area = "";
                            //obj.CredentialsRemark = "";
                            //obj.CredentialsStaffID = 0;
                            //obj.CredentialsState = 0;
                            obj.CredentialsTime = DateTime.Now;
                            obj.FinanceRunningsID = running.Id;

                            //obj.QueryArchivesRemark = "";
                            //obj.QueryArchivesStaffID = 0;
                            //obj.QueryArchivesState = 0;
                            obj.QueryArchivesTime = DateTime.Now;

                            //obj.TransferRemark = "";
                            //obj.TransferStaffID = 0;
                            //obj.TransferState = 0;
                            obj.TransferTime = DateTime.Now;

                            tmp = TransferContract.Insert(obj);
                            break;
                    }
                }

                var result = new AjaxResult();
                if (tmp <= 0)
                {
                    UserContract.Repository.UnitOfWork.Rollback();
                    result.Type = AjaxResultType.Success;
                    result.Content = "错误";
                }
                else
                {
                    UserContract.Repository.UnitOfWork.Commit();
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                }

                return Token(result, "");
            }
            catch (Exception ex)
            {
                UserContract.Repository.UnitOfWork.Rollback();
                var result = new AjaxResult("错误" + ex.Message, AjaxResultType.Error);
                return Token(result, "");
            }

        }


        //============================== 订单相关信息

        [Description("API-金融报备列表")]//分页请求
        public IHttpActionResult RunningList([FromBody]PageSearch entity)
        {
            //获取UserID
            int userid = BaseWebApiExt.GetUserIDByToken();
            var uModel = UserContract.GetByKey(userid);
            var query = Contract.Entities.Where(m => m.IntermediaryID == uModel.IntermediaryID);

            int cnt = UserRolesContract.Entities.Count(p => p.UserID == userid && p.IntermediaryRoleID == 1);//店长：1

            if (cnt == 0)
            {
                query = query.Where(x => x.UserID == userid);
            }
            if (!string.IsNullOrEmpty(entity.KeyWord))
            {
                query = query.Where(x => x.CustomerName.Contains(entity.KeyWord) || x.CustomerPhone.Contains(entity.KeyWord));
            }

            //数据总数
            var total = query.Count();

            //默认给一个排序的字段
            query = query.OrderByDescending(m => m.FilingTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)
                : Enumerable.Empty<FinanceRunnings>().AsQueryable();// null;

            List<FinanceRunningListViewModel> list = new List<FinanceRunningListViewModel>();
            foreach (var item in query.ToList())
            {
                var temp = new FinanceRunningListViewModel();
                temp.Id = item.Id;
                temp.UserName = item.UserName;
                temp.UserPhone = item.UserPhone;
                temp.HouseUserName = item.HouseOwnerName;
                temp.HouseUserPhone = item.HouseOwnerPhone;
                temp.CustomerUserName = item.CustomerName;
                temp.CustomerUserPhone = item.CustomerPhone;


                if (item.FinanceItemsID == (int)LoanType.商业贷款)
                {
                    var business = BusinessContract.Entities.Where(x => x.FinanceRunningsID == item.Id).FirstOrDefault();
                    temp.FinanceType = "房贷-" + LoanType.商业贷款.ToString();
                    temp.StateName = GetBusinessProgressState(item, business).Name;

                }
                else
                {
                    var itemObj = ItemContract.GetByKey(item.FinanceItemsID);
                    switch ((LoanType)itemObj.ParentID)
                    {
                        case LoanType.公积金贷款:
                            var funds = FundsContract.Entities.Where(x => x.FinanceRunningsID == item.Id).FirstOrDefault();
                            temp.FinanceType = "房贷-公积金-" + itemObj.Name;
                            temp.StateName = GetFundsProgressState(item, funds).Name;

                            break;
                        case LoanType.金融过户:
                            var transfer = TransferContract.Entities.Where(x => x.FinanceRunningsID == item.Id).FirstOrDefault();
                            temp.FinanceType = "房贷-过户-" + itemObj.Name;
                            temp.StateName = GetTransferProgressState(item, transfer).Name;

                            break;
                    }
                }
                temp.FilingTime = item.FilingTime.ToString("yy-MM-dd HH:mm");
                if (item.TransactStaffID != 0)
                {
                    var staff = StaffContract.GetByKey(item.TransactStaffID);
                    temp.TransactStaffName = staff.Name;
                    temp.TransactStaffPhone = staff.Phone;
                }
                else
                {
                    temp.TransactStaffName = "";
                    temp.TransactStaffPhone = "";
                }

                var brokerageRunningsModel = BrokerageRunningsContract.Entities
                    .Where(m => m.ModuleID == 5 && m.RunningsID == item.Id && m.BrokerageTypeID == 12).FirstOrDefault();
                if (brokerageRunningsModel != null)
                {
                    var totalAmount = brokerageRunningsModel.Amount;

                    var achieveAmount = BrokerageSettleAccountsRunningsContract.Entities
                        .Count(m => m.BrokerageRunningsID == brokerageRunningsModel.Id) > 0 ? BrokerageSettleAccountsRunningsContract.Entities
                        .Where(m => m.BrokerageRunningsID == brokerageRunningsModel.Id).Sum(x => x.Amount) : 0;

                    temp.Free = "已结" + achieveAmount.ToString("F2") + "  未结" + (totalAmount - achieveAmount).ToString("F2");
                }
                else

                    temp.Free = "待确认";


                list.Add(temp);
            }


            //var result = new AjaxResult("成功", AjaxResultType.Success, list); //AjaxResultRow
            var result = new AjaxResultRow("成功", AjaxResultType.Success, list, total);
            return Token(result, "");
        }

        [Description("API-金融报备订单")]//时间轴显示进度
        public IHttpActionResult RunningModel([FromBody]FinanceRunnings entity)
        {
            var model = Contract.GetByKey(entity.Id);
            var itemObj = ItemContract.GetByKey(model.FinanceItemsID);
            //State：1报备待审   2报备通过   3报备驳回   4已量尺   5已交定金   6已交首期工程款   7已验收完成   8已退
            //退单状态 ReturnState: 0 默认 1 退单待审 2 已退单
            //StyleID 模板选择： 1 报备+通过 2 报备+未通过 3 带看 4 完成
            //ModuleIcon 图标形状： 1 进行中，橘黄色 2 已完成，绿色 3 等待，灰色 4 拒绝，红色
            //颜色值： 橘黄色=#F57035 绿色=#3AB394 灰色 =#B3B3B3 红色=#F40001

            if (model != null)
            {
                var query = new FinanceRunningInfoModel();
                query.Id = model.Id;
                query.UserName = model.UserName;
                query.UserPhone = model.UserPhone;
                query.State = model.State;
                //query.Area = model.Area;//面积
                ////query.CustomerAddress = model.CustomerAddress;//地址
                //query.Remark = model.Remark;//备注

                query.TimelineShowList = new List<TimelineModel>();//流水步骤集合

                #region 报备
                //1 报备
                var FilingModel = new TimelineModel();
                FilingModel.SortID = 1;
                FilingModel.ModuleName = "报备";

                switch (model.FilingState)
                {
                    case 1://报备待审
                        FilingModel.StyleID = 1;//模板1                        
                        FilingModel.ModuleNameColor = "#F57035";//橘黄色
                        FilingModel.ModuleIcon = 1;//进行中
                        FilingModel.ModuleIconColor = "#F57035";//橘黄色
                        FilingModel.Title = "报备客户";
                        FilingModel.OperTime = model.FilingTime.ToString("yy-MM-dd HH:mm");
                        FilingModel.ViceTitle = "等待审核";
                        FilingModel.ViceTitleColor = "#F57035";//橘黄色
                        break;
                    case 3://报备驳回
                        FilingModel.StyleID = 2;//模板2
                        FilingModel.ModuleNameColor = "#F40001";//红色
                        FilingModel.ModuleIcon = 4;//拒绝
                        FilingModel.ModuleIconColor = "#F40001";//红色
                        FilingModel.Title = "报备客户";
                        FilingModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                        FilingModel.ViceTitle = "审核未通过";
                        FilingModel.ViceTitleColor = "#F40001";//红色
                        FilingModel.FilingAuditingRemark = model.FilingAuditingRemark;//报备审核说明
                        break;
                    case 2:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        FilingModel.StyleID = 1;//模板1
                        FilingModel.ModuleNameColor = "#3AB394";//绿色
                        FilingModel.ModuleIcon = 2;//已完成
                        FilingModel.ModuleIconColor = "#3AB394";//绿色
                        FilingModel.Title = "报备客户";
                        FilingModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                        FilingModel.ViceTitle = "审核通过";
                        FilingModel.ViceTitleColor = "#3AB394";//绿色
                        break;
                }

                query.TimelineShowList.Add(FilingModel);
                #endregion

                #region 备件--收集资料          
                var BandSawModel = new TimelineModel();
                BandSawModel.SortID = 2;
                BandSawModel.ModuleName = "备件";

                switch (model.CollectDataState)
                {
                    case 0://还没有 进入这个步骤
                        BandSawModel.StyleID = 4;//模板2
                        BandSawModel.ModuleNameColor = "#B3B3B3";//灰色
                        BandSawModel.ModuleIcon = 3;//拒绝
                        BandSawModel.ModuleIconColor = "#B3B3B3";//灰色
                        BandSawModel.Title = "收集资料";
                        BandSawModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                        BandSawModel.ViceTitle = "";
                        BandSawModel.ViceTitleColor = "#B3B3B3";//
                        BandSawModel.FilingAuditingRemark = "";//报备审核说明
                        break;
                    case 1://收集资料
                        BandSawModel.StyleID = 1;//模板1                        
                        BandSawModel.ModuleNameColor = "#F57035";//橘黄色
                        BandSawModel.ModuleIcon = 1;//进行中
                        BandSawModel.ModuleIconColor = "#F57035";//橘黄色
                        BandSawModel.Title = "收集资料";
                        BandSawModel.OperTime = model.CollectDataTime.ToString("yy-MM-dd HH:mm");
                        BandSawModel.ViceTitle = "";
                        BandSawModel.ViceTitleColor = "#F57035";//橘黄色
                        break;
                    case 3://报备驳回
                        BandSawModel.StyleID = 2;//模板2
                        BandSawModel.ModuleNameColor = "#F40001";//红色
                        BandSawModel.ModuleIcon = 4;//拒绝
                        BandSawModel.ModuleIconColor = "#F40001";//红色
                        BandSawModel.Title = "收集资料";
                        BandSawModel.OperTime = model.CollectDataTime.ToString("yy-MM-dd HH:mm");
                        BandSawModel.ViceTitle = "未通过";
                        BandSawModel.ViceTitleColor = "#F40001";//红色
                        BandSawModel.FilingAuditingRemark = model.CollectDataRemark;//报备审核说明
                        break;
                    case 2:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        BandSawModel.StyleID = 1;//模板1
                        BandSawModel.ModuleNameColor = "#3AB394";//绿色
                        BandSawModel.ModuleIcon = 2;//已完成
                        BandSawModel.ModuleIconColor = "#3AB394";//绿色
                        BandSawModel.Title = "收集资料";
                        BandSawModel.OperTime = model.CollectDataTime.ToString("yy-MM-dd HH:mm");
                        BandSawModel.ViceTitle = "已完成";
                        BandSawModel.ViceTitleColor = "#3AB394";//绿色
                        break;
                }
                query.TimelineShowList.Add(BandSawModel);
                #endregion

                //int tmp = 0;
                if ((LoanType)Enum.ToObject(typeof(LoanType), itemObj.Id) == LoanType.商业贷款)
                {

                    var business = BusinessContract.Entities.Where(x => x.FinanceRunningsID == model.Id).FirstOrDefault();
                    #region 3面签
                    //3 定金
                    var SubscribewModel = new TimelineModel();
                    SubscribewModel.SortID = 3;
                    SubscribewModel.ModuleName = "面签";

                    switch (business.ContractState)
                    {
                        case 0://还没有 进入这个步骤
                            SubscribewModel.StyleID = 4;//模板2
                            SubscribewModel.ModuleNameColor = "#B3B3B3";//灰色
                            SubscribewModel.ModuleIcon = 3;//
                            SubscribewModel.ModuleIconColor = "#B3B3B3";//灰色
                            SubscribewModel.Title = "面签";
                            SubscribewModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            SubscribewModel.ViceTitle = "";
                            SubscribewModel.ViceTitleColor = "#B3B3B3";//
                            SubscribewModel.FilingAuditingRemark = business.ContractRemark;//报备审核说明
                            break;
                        case 1://报备待审
                            SubscribewModel.StyleID = 1;//模板1                        
                            SubscribewModel.ModuleNameColor = "#F57035";//橘黄色
                            SubscribewModel.ModuleIcon = 1;//进行中
                            SubscribewModel.ModuleIconColor = "#F57035";//橘黄色
                            SubscribewModel.Title = "面签";
                            SubscribewModel.OperTime = business.ContractTime.ToString("yy-MM-dd HH:mm");
                            SubscribewModel.ViceTitle = "";
                            SubscribewModel.ViceTitleColor = "#F57035";//橘黄色
                            break;
                        case 3://报备驳回
                            SubscribewModel.StyleID = 2;//模板2
                            SubscribewModel.ModuleNameColor = "#F40001";//红色
                            SubscribewModel.ModuleIcon = 4;//拒绝
                            SubscribewModel.ModuleIconColor = "#F40001";//红色
                            SubscribewModel.Title = "面签";
                            SubscribewModel.OperTime = business.ContractTime.ToString("yy-MM-dd HH:mm");
                            SubscribewModel.ViceTitle = "未通过";
                            SubscribewModel.ViceTitleColor = "#F40001";//红色
                            SubscribewModel.FilingAuditingRemark = business.ContractRemark;//报备审核说明
                            break;

                        case 2:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                            SubscribewModel.StyleID = 1;//模板1
                            SubscribewModel.ModuleNameColor = "#3AB394";//绿色
                            SubscribewModel.ModuleIcon = 2;//已完成
                            SubscribewModel.ModuleIconColor = "#3AB394";//绿色
                            SubscribewModel.Title = "面签";
                            SubscribewModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            SubscribewModel.ViceTitle = "已通过";
                            SubscribewModel.ViceTitleColor = "#3AB394";//绿色
                            break;
                    }
                    query.TimelineShowList.Add(SubscribewModel);
                    #endregion

                    #region 4审批
                    var DownPaymentModel = new TimelineModel();
                    DownPaymentModel.SortID = 4;
                    DownPaymentModel.ModuleName = "审批";

                    switch (business.ApproveState)
                    {
                        case 0://还没有 进入这个步骤
                            DownPaymentModel.StyleID = 4;//模板2
                            DownPaymentModel.ModuleNameColor = "#B3B3B3";//灰色
                            DownPaymentModel.ModuleIcon = 3;
                            DownPaymentModel.ModuleIconColor = "#B3B3B3";//灰色
                            DownPaymentModel.Title = "审批";
                            DownPaymentModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            DownPaymentModel.ViceTitle = "";
                            DownPaymentModel.ViceTitleColor = "#B3B3B3";//红色
                            DownPaymentModel.FilingAuditingRemark = "";//
                            break;
                        case 1://报备待审
                            DownPaymentModel.StyleID = 1;//模板1                        
                            DownPaymentModel.ModuleNameColor = "#F57035";//橘黄色
                            DownPaymentModel.ModuleIcon = 1;//进行中
                            DownPaymentModel.ModuleIconColor = "#F57035";//橘黄色
                            DownPaymentModel.Title = "审批";
                            DownPaymentModel.OperTime = business.ApproveTime.ToString("yy-MM-dd HH:mm");
                            DownPaymentModel.ViceTitle = "";
                            DownPaymentModel.ViceTitleColor = "#F57035";//橘黄色
                            break;
                        case 3://报备驳回
                            DownPaymentModel.StyleID = 2;//模板2
                            DownPaymentModel.ModuleNameColor = "#F40001";//红色
                            DownPaymentModel.ModuleIcon = 4;//拒绝
                            DownPaymentModel.ModuleIconColor = "#F40001";//红色
                            DownPaymentModel.Title = "审批";
                            DownPaymentModel.OperTime = business.ApproveTime.ToString("yy-MM-dd HH:mm");
                            DownPaymentModel.ViceTitle = "未通过";
                            DownPaymentModel.ViceTitleColor = "#F40001";//红色
                            DownPaymentModel.FilingAuditingRemark = business.ApproveRemark;//报备审核说明
                            break;
                        case 2:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                            DownPaymentModel.StyleID = 1;//模板1
                            DownPaymentModel.ModuleNameColor = "#3AB394";//绿色
                            DownPaymentModel.ModuleIcon = 2;//已完成
                            DownPaymentModel.ModuleIconColor = "#3AB394";//绿色
                            DownPaymentModel.Title = "审批";
                            DownPaymentModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            DownPaymentModel.ViceTitle = "通过";
                            DownPaymentModel.ViceTitleColor = "#3AB394";//绿色
                            break;
                    }
                    query.TimelineShowList.Add(DownPaymentModel);
                    #endregion

                    #region 5放款
                    var CompleteModel = new TimelineModel();
                    CompleteModel.SortID = 5;
                    CompleteModel.ModuleName = "放款";

                    switch (business.ProvideState)
                    {
                        case 0://还没有 进入这个步骤
                            CompleteModel.StyleID = 4;//模板2
                            CompleteModel.ModuleNameColor = "#B3B3B3";//灰色
                            CompleteModel.ModuleIcon = 3;
                            CompleteModel.ModuleIconColor = "#B3B3B3";//灰色
                            CompleteModel.Title = "放款";
                            CompleteModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            CompleteModel.ViceTitle = "";
                            CompleteModel.ViceTitleColor = "#B3B3B3";//
                            CompleteModel.FilingAuditingRemark = "";//报备审核说明
                            break;
                        case 1://报备待审
                            CompleteModel.StyleID = 1;//模板1                        
                            CompleteModel.ModuleNameColor = "#F57035";//橘黄色
                            CompleteModel.ModuleIcon = 1;//进行中
                            CompleteModel.ModuleIconColor = "#F57035";//橘黄色
                            CompleteModel.Title = "放款";
                            CompleteModel.OperTime = business.ProvideTime.ToString("yy-MM-dd HH:mm");
                            CompleteModel.ViceTitle = "";
                            CompleteModel.ViceTitleColor = "#F57035";//橘黄色
                            break;
                        case 3://报备驳回
                            CompleteModel.StyleID = 2;//模板2
                            CompleteModel.ModuleNameColor = "#F40001";//红色
                            CompleteModel.ModuleIcon = 4;//拒绝
                            CompleteModel.ModuleIconColor = "#F40001";//红色
                            CompleteModel.Title = "放款";
                            CompleteModel.OperTime = business.ProvideTime.ToString("yy-MM-dd HH:mm");
                            CompleteModel.ViceTitle = "未通过";
                            CompleteModel.ViceTitleColor = "#F40001";//红色
                            CompleteModel.FilingAuditingRemark = business.ProvideRemark;//报备审核说明
                            break;
                        case 2:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                            CompleteModel.StyleID = 4;//模板1
                            CompleteModel.ModuleNameColor = "#3AB394";//绿色
                            CompleteModel.ModuleIcon = 2;//已完成
                            CompleteModel.ModuleIconColor = "#3AB394";//绿色
                            CompleteModel.Title = "放款";
                            CompleteModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                            CompleteModel.ViceTitle = "通过";
                            CompleteModel.ViceTitleColor = "#3AB394";//绿色
                            break;
                    }
                    query.TimelineShowList.Add(CompleteModel);
                    #endregion
                }
                else
                {
                    switch ((LoanType)Enum.ToObject(typeof(LoanType), itemObj.ParentID))
                    {
                        case LoanType.公积金贷款:
                            var funds = FundsContract.Entities.Where(x => x.FinanceRunningsID == model.Id).FirstOrDefault();
                            #region 3面签
                            //3 定金
                            var SubscribewModel = new TimelineModel();
                            SubscribewModel.SortID = 3;
                            SubscribewModel.ModuleName = "面签";

                            switch (funds.ContractState)
                            {
                                case 0://还没有 进入这个步骤
                                    SubscribewModel.StyleID = 4;//模板2
                                    SubscribewModel.ModuleNameColor = "#B3B3B3";//灰色
                                    SubscribewModel.ModuleIcon = 3;//
                                    SubscribewModel.ModuleIconColor = "#B3B3B3";//灰色
                                    SubscribewModel.Title = "面签";
                                    SubscribewModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    SubscribewModel.ViceTitle = "";
                                    SubscribewModel.ViceTitleColor = "#B3B3B3";//
                                    SubscribewModel.FilingAuditingRemark = funds.ContractRemark;//报备审核说明
                                    break;
                                case 1://报备待审
                                    SubscribewModel.StyleID = 1;//模板1                        
                                    SubscribewModel.ModuleNameColor = "#F57035";//橘黄色
                                    SubscribewModel.ModuleIcon = 1;//进行中
                                    SubscribewModel.ModuleIconColor = "#F57035";//橘黄色
                                    SubscribewModel.Title = "面签";
                                    SubscribewModel.OperTime = funds.ContractTime.ToString("yy-MM-dd HH:mm");
                                    SubscribewModel.ViceTitle = "";
                                    SubscribewModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    SubscribewModel.StyleID = 2;//模板2
                                    SubscribewModel.ModuleNameColor = "#F40001";//红色
                                    SubscribewModel.ModuleIcon = 4;//拒绝
                                    SubscribewModel.ModuleIconColor = "#F40001";//红色
                                    SubscribewModel.Title = "面签";
                                    SubscribewModel.OperTime = funds.ContractTime.ToString("yy-MM-dd HH:mm");
                                    SubscribewModel.ViceTitle = "未通过";
                                    SubscribewModel.ViceTitleColor = "#F40001";//红色
                                    SubscribewModel.FilingAuditingRemark = funds.ContractRemark;//报备审核说明
                                    break;

                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    SubscribewModel.StyleID = 1;//模板1
                                    SubscribewModel.ModuleNameColor = "#3AB394";//绿色
                                    SubscribewModel.ModuleIcon = 2;//已完成
                                    SubscribewModel.ModuleIconColor = "#3AB394";//绿色
                                    SubscribewModel.Title = "面签";
                                    SubscribewModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    SubscribewModel.ViceTitle = "已通过";
                                    SubscribewModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(SubscribewModel);
                            #endregion

                            #region 4抵押
                            var DownPaymentModel = new TimelineModel();
                            DownPaymentModel.SortID = 4;
                            DownPaymentModel.ModuleName = "审批";

                            switch (funds.MortgageState)
                            {
                                case 0://还没有 进入这个步骤
                                    DownPaymentModel.StyleID = 4;//模板2
                                    DownPaymentModel.ModuleNameColor = "#B3B3B3";//灰色
                                    DownPaymentModel.ModuleIcon = 3;
                                    DownPaymentModel.ModuleIconColor = "#B3B3B3";//灰色
                                    DownPaymentModel.Title = "审批";
                                    DownPaymentModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    DownPaymentModel.ViceTitle = "";
                                    DownPaymentModel.ViceTitleColor = "#B3B3B3";//红色
                                    DownPaymentModel.FilingAuditingRemark = "";//
                                    break;
                                case 1://报备待审
                                    DownPaymentModel.StyleID = 1;//模板1                        
                                    DownPaymentModel.ModuleNameColor = "#F57035";//橘黄色
                                    DownPaymentModel.ModuleIcon = 1;//进行中
                                    DownPaymentModel.ModuleIconColor = "#F57035";//橘黄色
                                    DownPaymentModel.Title = "审批";
                                    DownPaymentModel.OperTime = funds.MortgageTime.ToString("yy-MM-dd HH:mm");
                                    DownPaymentModel.ViceTitle = "";
                                    DownPaymentModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    DownPaymentModel.StyleID = 2;//模板2
                                    DownPaymentModel.ModuleNameColor = "#F40001";//红色
                                    DownPaymentModel.ModuleIcon = 4;//拒绝
                                    DownPaymentModel.ModuleIconColor = "#F40001";//红色
                                    DownPaymentModel.Title = "审批";
                                    DownPaymentModel.OperTime = funds.MortgageTime.ToString("yy-MM-dd HH:mm");
                                    DownPaymentModel.ViceTitle = "未通过";
                                    DownPaymentModel.ViceTitleColor = "#F40001";//红色
                                    DownPaymentModel.FilingAuditingRemark = funds.MortgageRemark;//报备审核说明
                                    break;
                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    DownPaymentModel.StyleID = 1;//模板1
                                    DownPaymentModel.ModuleNameColor = "#3AB394";//绿色
                                    DownPaymentModel.ModuleIcon = 2;//已完成
                                    DownPaymentModel.ModuleIconColor = "#3AB394";//绿色
                                    DownPaymentModel.Title = "审批";
                                    DownPaymentModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    DownPaymentModel.ViceTitle = "通过";
                                    DownPaymentModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(DownPaymentModel);
                            #endregion

                            #region 5放款
                            var CompleteModel = new TimelineModel();
                            CompleteModel.SortID = 5;
                            CompleteModel.ModuleName = "放款";

                            switch (funds.ProvideState)
                            {
                                case 0://还没有 进入这个步骤
                                    CompleteModel.StyleID = 4;//模板2
                                    CompleteModel.ModuleNameColor = "#B3B3B3";//灰色
                                    CompleteModel.ModuleIcon = 3;
                                    CompleteModel.ModuleIconColor = "#B3B3B3";//灰色
                                    CompleteModel.Title = "放款";
                                    CompleteModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    CompleteModel.ViceTitle = "";
                                    CompleteModel.ViceTitleColor = "#B3B3B3";//
                                    CompleteModel.FilingAuditingRemark = "";//报备审核说明
                                    break;
                                case 1://报备待审
                                    CompleteModel.StyleID = 1;//模板1                        
                                    CompleteModel.ModuleNameColor = "#F57035";//橘黄色
                                    CompleteModel.ModuleIcon = 1;//进行中
                                    CompleteModel.ModuleIconColor = "#F57035";//橘黄色
                                    CompleteModel.Title = "放款";
                                    CompleteModel.OperTime = funds.ProvideTime.ToString("yy-MM-dd HH:mm");
                                    CompleteModel.ViceTitle = "";
                                    CompleteModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    CompleteModel.StyleID = 2;//模板2
                                    CompleteModel.ModuleNameColor = "#F40001";//红色
                                    CompleteModel.ModuleIcon = 4;//拒绝
                                    CompleteModel.ModuleIconColor = "#F40001";//红色
                                    CompleteModel.Title = "放款";
                                    CompleteModel.OperTime = funds.ProvideTime.ToString("yy-MM-dd HH:mm");
                                    CompleteModel.ViceTitle = "未通过";
                                    CompleteModel.ViceTitleColor = "#F40001";//红色
                                    CompleteModel.FilingAuditingRemark = funds.ProvideRemark;//报备审核说明
                                    break;
                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    CompleteModel.StyleID = 4;//模板1
                                    CompleteModel.ModuleNameColor = "#3AB394";//绿色
                                    CompleteModel.ModuleIcon = 2;//已完成
                                    CompleteModel.ModuleIconColor = "#3AB394";//绿色
                                    CompleteModel.Title = "放款";
                                    CompleteModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    CompleteModel.ViceTitle = "通过";
                                    CompleteModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(CompleteModel);
                            #endregion
                            break;
                        case LoanType.金融过户:
                            var transfer = TransferContract.Entities.Where(x => x.FinanceRunningsID == model.Id).FirstOrDefault();
                            #region 3查档报税
                            //3 定金
                            var T_SubscribewModel = new TimelineModel();
                            T_SubscribewModel.SortID = 3;
                            T_SubscribewModel.ModuleName = "查档报税";

                            switch (transfer.QueryArchivesState)
                            {
                                case 0://还没有 进入这个步骤
                                    T_SubscribewModel.StyleID = 4;//模板2
                                    T_SubscribewModel.ModuleNameColor = "#B3B3B3";//灰色
                                    T_SubscribewModel.ModuleIcon = 3;//
                                    T_SubscribewModel.ModuleIconColor = "#B3B3B3";//灰色
                                    T_SubscribewModel.Title = "查档报税";
                                    T_SubscribewModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_SubscribewModel.ViceTitle = "";
                                    T_SubscribewModel.ViceTitleColor = "#B3B3B3";//
                                    T_SubscribewModel.FilingAuditingRemark = "";//报备审核说明
                                    break;
                                case 1://报备待审
                                    T_SubscribewModel.StyleID = 1;//模板1                        
                                    T_SubscribewModel.ModuleNameColor = "#F57035";//橘黄色
                                    T_SubscribewModel.ModuleIcon = 1;//进行中
                                    T_SubscribewModel.ModuleIconColor = "#F57035";//橘黄色
                                    T_SubscribewModel.Title = "查档报税";
                                    T_SubscribewModel.OperTime = transfer.QueryArchivesTime.ToString("yy-MM-dd HH:mm");
                                    T_SubscribewModel.ViceTitle = "";
                                    T_SubscribewModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    T_SubscribewModel.StyleID = 2;//模板2
                                    T_SubscribewModel.ModuleNameColor = "#F40001";//红色
                                    T_SubscribewModel.ModuleIcon = 4;//拒绝
                                    T_SubscribewModel.ModuleIconColor = "#F40001";//红色
                                    T_SubscribewModel.Title = "查档报税";
                                    T_SubscribewModel.OperTime = transfer.QueryArchivesTime.ToString("yy-MM-dd HH:mm");
                                    T_SubscribewModel.ViceTitle = "未通过";
                                    T_SubscribewModel.ViceTitleColor = "#F40001";//红色
                                    T_SubscribewModel.FilingAuditingRemark = transfer.QueryArchivesRemark;//报备审核说明
                                    break;

                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    T_SubscribewModel.StyleID = 1;//模板1
                                    T_SubscribewModel.ModuleNameColor = "#3AB394";//绿色
                                    T_SubscribewModel.ModuleIcon = 2;//已完成
                                    T_SubscribewModel.ModuleIconColor = "#3AB394";//绿色
                                    T_SubscribewModel.Title = "面签";
                                    T_SubscribewModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_SubscribewModel.ViceTitle = "已通过";
                                    T_SubscribewModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(T_SubscribewModel);
                            #endregion

                            #region 4过户
                            var T_DownPaymentModel = new TimelineModel();
                            T_DownPaymentModel.SortID = 4;
                            T_DownPaymentModel.ModuleName = "过户";

                            switch (transfer.TransferState)
                            {
                                case 0://还没有 进入这个步骤
                                    T_DownPaymentModel.StyleID = 4;//模板2
                                    T_DownPaymentModel.ModuleNameColor = "#B3B3B3";//灰色
                                    T_DownPaymentModel.ModuleIcon = 3;
                                    T_DownPaymentModel.ModuleIconColor = "#B3B3B3";//灰色
                                    T_DownPaymentModel.Title = "过户";
                                    T_DownPaymentModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_DownPaymentModel.ViceTitle = "";
                                    T_DownPaymentModel.ViceTitleColor = "#B3B3B3";//红色
                                    T_DownPaymentModel.FilingAuditingRemark = "";//
                                    break;
                                case 1://报备待审
                                    T_DownPaymentModel.StyleID = 1;//模板1                        
                                    T_DownPaymentModel.ModuleNameColor = "#F57035";//橘黄色
                                    T_DownPaymentModel.ModuleIcon = 1;//进行中
                                    T_DownPaymentModel.ModuleIconColor = "#F57035";//橘黄色
                                    T_DownPaymentModel.Title = "过户";
                                    T_DownPaymentModel.OperTime = transfer.TransferTime.ToString("yy-MM-dd HH:mm");
                                    T_DownPaymentModel.ViceTitle = "";
                                    T_DownPaymentModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    T_DownPaymentModel.StyleID = 2;//模板2
                                    T_DownPaymentModel.ModuleNameColor = "#F40001";//红色
                                    T_DownPaymentModel.ModuleIcon = 4;//拒绝
                                    T_DownPaymentModel.ModuleIconColor = "#F40001";//红色
                                    T_DownPaymentModel.Title = "过户";
                                    T_DownPaymentModel.OperTime = transfer.TransferTime.ToString("yy-MM-dd HH:mm");
                                    T_DownPaymentModel.ViceTitle = "未通过";
                                    T_DownPaymentModel.ViceTitleColor = "#F40001";//红色
                                    T_DownPaymentModel.FilingAuditingRemark = transfer.TransferRemark;//报备审核说明
                                    break;
                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    T_DownPaymentModel.StyleID = 1;//模板1
                                    T_DownPaymentModel.ModuleNameColor = "#3AB394";//绿色
                                    T_DownPaymentModel.ModuleIcon = 2;//已完成
                                    T_DownPaymentModel.ModuleIconColor = "#3AB394";//绿色
                                    T_DownPaymentModel.Title = "审批";
                                    T_DownPaymentModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_DownPaymentModel.ViceTitle = "通过";
                                    T_DownPaymentModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(T_DownPaymentModel);
                            #endregion

                            #region 5下证
                            var T_CompleteModel = new TimelineModel();
                            T_CompleteModel.SortID = 5;
                            T_CompleteModel.ModuleName = "下证";

                            switch (transfer.CredentialsState)
                            {
                                case 0://还没有 进入这个步骤
                                    T_CompleteModel.StyleID = 4;//模板2
                                    T_CompleteModel.ModuleNameColor = "#B3B3B3";//灰色
                                    T_CompleteModel.ModuleIcon = 3;
                                    T_CompleteModel.ModuleIconColor = "#B3B3B3";//灰色
                                    T_CompleteModel.Title = "下证";
                                    T_CompleteModel.OperTime = "";// model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_CompleteModel.ViceTitle = "";
                                    T_CompleteModel.ViceTitleColor = "#B3B3B3";//
                                    T_CompleteModel.FilingAuditingRemark = "";//报备审核说明
                                    break;
                                case 1://报备待审
                                    T_CompleteModel.StyleID = 1;//模板1                        
                                    T_CompleteModel.ModuleNameColor = "#F57035";//橘黄色
                                    T_CompleteModel.ModuleIcon = 1;//进行中
                                    T_CompleteModel.ModuleIconColor = "#F57035";//橘黄色
                                    T_CompleteModel.Title = "下证";
                                    T_CompleteModel.OperTime = transfer.CredentialsTime.ToString("yy-MM-dd HH:mm");
                                    T_CompleteModel.ViceTitle = "";
                                    T_CompleteModel.ViceTitleColor = "#F57035";//橘黄色
                                    break;
                                case 3://报备驳回
                                    T_CompleteModel.StyleID = 2;//模板2
                                    T_CompleteModel.ModuleNameColor = "#F40001";//红色
                                    T_CompleteModel.ModuleIcon = 4;//拒绝
                                    T_CompleteModel.ModuleIconColor = "#F40001";//红色
                                    T_CompleteModel.Title = "下证";
                                    T_CompleteModel.OperTime = transfer.CredentialsTime.ToString("yy-MM-dd HH:mm");
                                    T_CompleteModel.ViceTitle = "未通过";
                                    T_CompleteModel.ViceTitleColor = "#F40001";//红色
                                    T_CompleteModel.FilingAuditingRemark = transfer.CredentialsRemark;//报备审核说明
                                    break;
                                case 2:
                                case 4:
                                case 5:
                                case 6:
                                case 7:
                                    T_CompleteModel.StyleID = 4;//模板1
                                    T_CompleteModel.ModuleNameColor = "#3AB394";//绿色
                                    T_CompleteModel.ModuleIcon = 2;//已完成
                                    T_CompleteModel.ModuleIconColor = "#3AB394";//绿色
                                    T_CompleteModel.Title = "放款";
                                    T_CompleteModel.OperTime = model.FilingAuditingTime.ToString("yy-MM-dd HH:mm");
                                    T_CompleteModel.ViceTitle = "通过";
                                    T_CompleteModel.ViceTitleColor = "#3AB394";//绿色
                                    break;
                            }
                            query.TimelineShowList.Add(T_CompleteModel);
                            #endregion
                            break;
                    }
                }
                //跟进信息
                //====================================================================================================

                query.MessageBoardsShowList = (from m in MessageBoardsContract.Entities
                                               join n in StaffContract.Entities
                                               on m.StaffID equals n.Id into tempb
                                               from b in tempb.DefaultIfEmpty()
                                               where m.MessageBoardsType == 7 && m.InterfixID == model.Id && m.IsDeleted == false
                                               select new
                                               {
                                                   StaffName = b.Name == null ? "" : b.Name,
                                                   Content = m.Content,
                                                   CreatedTime = m.CreatedTime
                                               }).ToList()
                                               .Select(m => new MessageBoardsShowModel
                                               {
                                                   StaffName = m.StaffName,
                                                   Content = m.Content,
                                                   CreatedTime = m.CreatedTime.ToString("yy-MM-dd HH:mm")
                                               }).ToList();//跟进信息集合


                //====================================================================================================




                //佣金计算
                decimal totalAmount = 0;//应得佣金
                decimal taxAmount = 0;//税款
                decimal achieveAmount = 0;//已结佣金                
                decimal unAchieveAmount = 0;//未结佣金
                int brokerageState = 1;//佣金流水状态

                //应得佣金 参照表BrokerageType
                var brokerageRunningsModel = BrokerageRunningsContract.Entities.Where(m => m.ModuleID == 5 && m.RunningsID == model.Id && m.BrokerageTypeID == 12).FirstOrDefault();
                if (brokerageRunningsModel != null)
                {
                    totalAmount = brokerageRunningsModel.Amount;
                    taxAmount = brokerageRunningsModel.Tax;
                    brokerageState = brokerageRunningsModel.State;

                    achieveAmount = BrokerageSettleAccountsRunningsContract.Entities.Count(m => m.BrokerageRunningsID == brokerageRunningsModel.Id) > 0 ?
                        BrokerageSettleAccountsRunningsContract.Entities.Where(m => m.BrokerageRunningsID == brokerageRunningsModel.Id).Sum(x => x.Amount) : 0;

                    //var brokerageSettleAccountsRunningsList = BrokerageSettleAccountsRunningsContract.Entities.Where(m => m.BrokerageRunningsID == brokerageRunningsModel.Id).ToList();
                    //foreach (var item in brokerageSettleAccountsRunningsList)
                    //{
                    //    achieveAmount += item.Amount;
                    //}
                }
                unAchieveAmount = totalAmount - taxAmount - achieveAmount;

                query.TotalAmount = totalAmount.ToString("F2") + "元";
                query.TaxAmount = taxAmount.ToString("F2") + "元";
                query.AchieveAmount = achieveAmount.ToString("F2") + "元";
                query.UnAchieveAmount = unAchieveAmount.ToString("F2") + "元";
                switch (brokerageState)
                {
                    case 0:
                    case 1:
                        query.BrokerageState = 2;
                        break;
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                        query.BrokerageState = 1;
                        break;
                }

                var result = new AjaxResult("成功", AjaxResultType.Success, query);
                return Token(result, "");
            }
            else
            {
                var result = new AjaxResult("失败", AjaxResultType.Error);
                return Token(result, "");
            }

        }

        #endregion



        //------------------------------------------------------------------------

        private BaseRemark GetBusinessProgressState(FinanceRunnings entity, FinanceBusinessLoanRunnings loanEntity)
        {
            //拒绝时，显示当前操作时间
            BaseRemark stepIdx = new BaseRemark();

            if (entity.FilingState == 1)//报备待审核
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备待审核", Value = entity.FilingTime };//0;
            }
            else if (entity.FilingState == 3)//拒绝时，显示当前操作时间
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备审核" + Progress_State.已终止, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//0;
            }
            else if (entity.FilingState == 2)
            {//报备审核已完成
             //stepIdx = new BaseName() { Id = 0, Name = "报备待审核" };//1;
                if (entity.CollectDataState == 1)//搜集资料待完成
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.进行中, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//1;
                }
                else if (entity.CollectDataState == 3)//资料不全，被终止
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.已终止, Value = entity.CollectDataTime, Remark = entity.CollectDataRemark };//0;
                }
                else if (entity.CollectDataState == 2)////搜集资料已完成
                {
                    //stepIdx = new BaseRemark() { Id = 2, Name = "资料搜集 已完成", Value = frViewModel.FilingAuditingTime, Remark= entity.CollectDataRemark };//2;
                    if (loanEntity.ContractState == 0)//分配办件人
                    {
                        stepIdx = new BaseRemark() { Id = 2, Name = "备件" + Progress_State.已完成, Remark = entity.CollectDataRemark };//2;
                        if (entity.TransactStaffID != 0)
                        {
                            stepIdx = new BaseRemark() { Id = 3, Name = "面签准备中", Remark = loanEntity.ContractRemark };//3;
                        }
                    }
                    else if (loanEntity.ContractState == 1)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "面签" + Progress_State.进行中, Value = loanEntity.ContractTime, Remark = loanEntity.ContractRemark };//4;
                    }
                    else if (loanEntity.ContractState == 3)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "面签" + Progress_State.已终止, Value = loanEntity.ContractTime, Remark = loanEntity.ContractRemark };//4;
                    }
                    else if (loanEntity.ContractState == 2)
                    {
                        //stepIdx = new BaseRemark() { Id = 5, Name = "面签 已完成", Value = loanEntity.ContractTime, Remark = loanEntity.ApproveRemark };//5;
                        if (loanEntity.ApproveState == 1)//审批进行中
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "面签" + Progress_State.已完成, Value = loanEntity.ContractTime, Remark = loanEntity.ProvideRemark };//5;
                        }
                        else if (loanEntity.ApproveState == 3)
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "审批" + Progress_State.已终止, Value = loanEntity.ApproveTime, Remark = loanEntity.ProvideRemark };//5;
                        }
                        else if (loanEntity.ApproveState == 2)
                        {
                            //stepIdx = new BaseRemark() { Id = 6, Name = "审批 已完成", Value = loanEntity.ApproveTime, Remark = loanEntity.ProvideRemark };//6;
                            if (loanEntity.ProvideState == 1)
                            {
                                stepIdx = new BaseRemark() { Id = 6, Name = "放款" + Progress_State.进行中, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };//6;
                            }
                            else if (loanEntity.ProvideState == 2)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "放款" + Progress_State.已完成, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };// 7;
                            }
                            else if (loanEntity.ProvideState == 3)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "放款" + Progress_State.已终止, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };//7;
                            }
                        }
                    }
                }
            }

            return stepIdx;

        }

        private BaseRemark GetFundsProgressState(FinanceRunnings entity, FinancePublicAccumulationFundsRunnings loanEntity)
        {

            BaseRemark stepIdx = new BaseRemark();//  0;


            if (entity.FilingState == 1)//报备待审核
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备待审核", Value = entity.FilingTime };//0;
            }
            else if (entity.FilingState == 3)//拒绝时，显示当前操作时间
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备审核" + Progress_State.已终止, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//0;
            }
            else if (entity.FilingState == 2)
            {//报备审核已完成
             //stepIdx = 1;
                if (entity.CollectDataState == 1)//搜集资料待完成
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.进行中, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//1;
                }
                else if (entity.CollectDataState == 3)//资料不全，被终止
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.已终止, Value = entity.CollectDataTime, Remark = entity.CollectDataRemark };//0;
                }
                else if (entity.CollectDataState == 2)////搜集资料已完成
                {
                    //stepIdx = 2;
                    if (loanEntity.ContractState == 0)//分配办件人
                    {
                        stepIdx = new BaseRemark() { Id = 2, Name = "备件" + Progress_State.已完成, Remark = entity.CollectDataRemark };//2;
                        if (entity.TransactStaffID != 0)
                        {
                            stepIdx = new BaseRemark() { Id = 3, Name = "面签准备中", Remark = loanEntity.ContractRemark };//3;
                        }
                    }
                    else if (loanEntity.ContractState == 1)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "面签" + Progress_State.进行中, Value = loanEntity.ContractTime, Remark = loanEntity.ContractRemark };//4;
                    }
                    else if (loanEntity.ContractState == 3)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "面签" + Progress_State.已终止, Value = loanEntity.ContractTime, Remark = loanEntity.ContractRemark };//4;
                    }
                    else if (loanEntity.ContractState == 2)
                    {
                        //stepIdx = 5;
                        if (loanEntity.MortgageState == 1)//审批进行中
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "面签" + Progress_State.已完成, Value = loanEntity.ContractTime, Remark = loanEntity.MortgageRemark };//5;
                        }
                        else if (loanEntity.MortgageState == 3)
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "审批" + Progress_State.已终止, Value = loanEntity.MortgageTime, Remark = loanEntity.MortgageRemark };//5;
                        }
                        else if (loanEntity.MortgageState == 2)
                        {
                            //stepIdx = 6;
                            if (loanEntity.ProvideState == 1)
                            {
                                stepIdx = new BaseRemark() { Id = 6, Name = "放款" + Progress_State.进行中, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };//6;
                            }
                            else if (loanEntity.ProvideState == 2)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "放款" + Progress_State.已完成, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };// 7;
                            }
                            else if (loanEntity.ProvideState == 3)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "放款" + Progress_State.已终止, Value = loanEntity.ProvideTime, Remark = loanEntity.ProvideRemark };//7;
                            }
                        }

                    }

                }
            }

            return stepIdx;

        }

        private BaseRemark GetTransferProgressState(FinanceRunnings entity, FinanceTransferRunnings loanEntity)
        {

            BaseRemark stepIdx = new BaseRemark();//  0;

            if (entity.FilingState == 1)//报备待审核
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备待审核", Value = entity.FilingTime };//0
            }
            else if (entity.FilingState == 3)
            {
                stepIdx = new BaseRemark() { Id = 0, Name = "报备审核" + Progress_State.已终止, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//0;
            }
            else if (entity.FilingState == 2)
            {//报备审核已完成
             //stepIdx = new BaseName() { Id = 0, Name = "报备待审核" };//1;
                if (entity.CollectDataState == 1)//搜集资料待完成
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.进行中, Value = entity.FilingAuditingTime, Remark = entity.FilingAuditingRemark };//1;
                }
                else if (entity.CollectDataState == 3)//资料不全，被终止
                {
                    stepIdx = new BaseRemark() { Id = 1, Name = "备件" + Progress_State.已终止, Value = entity.CollectDataTime, Remark = entity.CollectDataRemark };//0;
                }
                else if (entity.CollectDataState == 2)////搜集资料已完成
                {
                    //stepIdx = new BaseRemark() { Id = 2, Name = "资料搜集 已完成", Value = frViewModel.FilingAuditingTime, Remark = entity.CollectDataRemark };//2;
                    if (loanEntity.QueryArchivesState == 0)//分配办件人
                    {
                        stepIdx = new BaseRemark() { Id = 2, Name = "备件" + Progress_State.已完成, Remark = entity.CollectDataRemark };//2;
                        if (entity.TransactStaffID != 0)
                        {
                            stepIdx = new BaseRemark() { Id = 3, Name = "面签准备中", Remark = loanEntity.QueryArchivesRemark };//3;
                        }
                    }
                    else if (loanEntity.QueryArchivesState == 1)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "查档" + Progress_State.进行中, Value = loanEntity.QueryArchivesTime, Remark = loanEntity.QueryArchivesRemark };//4;
                    }
                    else if (loanEntity.QueryArchivesState == 3)
                    {
                        stepIdx = new BaseRemark() { Id = 4, Name = "查档" + Progress_State.已终止, Value = loanEntity.QueryArchivesTime, Remark = loanEntity.QueryArchivesRemark };//4;
                    }
                    else if (loanEntity.QueryArchivesState == 2)
                    {
                        //stepIdx = new BaseRemark() { Id = 5, Name = "过户 进行中", Value = frViewModel.ContractTime, Remark = loanEntity.QueryArchivesRemark };//5;
                        if (loanEntity.TransferState == 1)//审批进行中
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "过户" + Progress_State.已完成, Remark = loanEntity.TransferRemark };//5;
                        }
                        else if (loanEntity.TransferState == 3)
                        {
                            stepIdx = new BaseRemark() { Id = 5, Name = "过户" + Progress_State.已终止, Value = loanEntity.TransferTime, Remark = loanEntity.TransferRemark };//5;
                        }
                        else if (loanEntity.TransferState == 2)
                        {
                            //stepIdx = new BaseRemark() { Id = 6, Name = "过户 已完成", Value = frViewModel.ApproveTime, Remark = loanEntity.TransferRemark };//6;
                            if (loanEntity.CredentialsState == 1)
                            {
                                stepIdx = new BaseRemark() { Id = 6, Name = "下证" + Progress_State.进行中, Value = loanEntity.CredentialsTime, Remark = loanEntity.CredentialsRemark };//6;
                            }
                            else if (loanEntity.CredentialsState == 2)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "下证" + Progress_State.已完成, Value = loanEntity.CredentialsTime, Remark = loanEntity.CredentialsRemark };// 7;
                            }
                            else if (loanEntity.CredentialsState == 3)
                            {
                                stepIdx = new BaseRemark() { Id = 7, Name = "下证" + Progress_State.已终止, Value = loanEntity.CredentialsTime, Remark = loanEntity.CredentialsRemark };//7;
                            }
                        }
                    }
                }
            }

            return stepIdx;

        }
    }
}